package com.ruoyi.web.controller.riskAssessment;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.riskAssessment.domain.RiskTrain;
import com.ruoyi.riskAssessment.dto.RiskTrainScoreDto;
import com.ruoyi.riskAssessment.dto.RiskTrainScoreListDto;
import com.ruoyi.riskAssessment.service.IRiskTrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 风险-培训管理Controller
 *
 * @author lth
 * @date 2023-12-15
 */
@RestController
@RequestMapping("/riskAssessment/train")
public class RiskTrainController extends BaseController
{
    @Autowired
    private IRiskTrainService riskTrainService;

    /**
     * 查询风险-培训管理列表
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:train:list')")
    @GetMapping("/list")
    public TableDataInfo list(RiskTrain riskTrain)
    {
        startPage();
        List<RiskTrain> list = riskTrainService.selectRiskTrainList(riskTrain);
        return getDataTable(list);
    }

    /**
     * 导出风险-培训管理列表
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:train:export')")
    @Log(title = "风险-培训管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RiskTrain riskTrain)
    {
        List<RiskTrain> list = riskTrainService.selectRiskTrainList(riskTrain);
        ExcelUtil<RiskTrain> util = new ExcelUtil<RiskTrain>(RiskTrain.class);
        util.exportExcel(response, list, "风险-培训管理数据");
    }

    /**
     * 获取风险-培训管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:train:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(riskTrainService.selectRiskTrainById(id));
    }

    /**
     * 新增风险-培训管理
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:train:add')")
    @Log(title = "风险-培训管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RiskTrain riskTrain)
    {
        long id = new SnowflakeIdUtils(5, 8).nextId();
        riskTrain.setId(id);
        riskTrain.setCode("HDTR-"+(id+1));
        riskTrain.setCreateAt(new Date());
        return toAjax(riskTrainService.insertRiskTrain(riskTrain));
    }

    /**
     * 修改风险-培训管理
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:train:edit')")
    @Log(title = "风险-培训管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RiskTrain riskTrain)
    {
        return toAjax(riskTrainService.updateRiskTrain(riskTrain));
    }

    /**
     * 删除风险-培训管理
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:train:remove')")
    @Log(title = "风险-培训管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(riskTrainService.deleteRiskTrainByIds(ids));
    }


    /**
     * 风险-培训管理-打分
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:train:query')")
    @GetMapping("/scoring/{id}")
    public TableDataInfo scoring(@PathVariable Long id)
    {
        startPage();
        //List<RiskTrainScoreDto> list = riskTrainService.scoring(id);
        List<RiskTrainScoreListDto> scoreList = riskTrainService.scoring(id);
        return getDataTable(scoreList);
    }


}
